﻿using WRJClient.DAL;
using WRJClient.Model;
using Newtonsoft.Json;
using WRJClient.Common;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WRJClient.ViewModel;

namespace WRJClient.Net
{
    public class WorkStationMessageProcessor : IMessageProcessor
    {
        public void Process(TopicData topicData)
        {
            switch (topicData.eventId)
            {
                case 1://工作站新建事件
                    WorkStationAddProcess(topicData);
                    break;
                case 2://工作站删除事件
                    WorkStationDelProcess(topicData);
                    break;
                case 3://工作站修改事件
                    WorkStationDelProcess(topicData);
                    break;
                case 4://工作站心跳事件
                    WorkStationHeartbeatProcess(topicData);
                    break;
                case 5://工作站上线事件
                    WorkStationOnlineProcess(topicData);
                    break;
                case 6://工作站下线事件
                    WorkStationOfflineProcess(topicData);
                    break;
                case 7://工作站建链事件
                    WorkStationConnectedProcess(topicData);
                    break;
                case 8://工作站断链事件
                    WorkStationDisconnectedProcess(topicData);
                    break;
                default:
                    break;
            }
        }

        private void WorkStationAddProcess(TopicData topicData)
        {
            //MessageManager.Instance.Info(string.Format("新建工作站[{0}]!", topicData.id));
        }
        private void WorkStationDelProcess(TopicData topicData)
        {
            //MessageManager.Instance.Info(string.Format("删除工作站[{0}]!", topicData.id));
        }
        private void WorkStationUpdateProcess(TopicData topicData)
        {
            //MessageManager.Instance.Info(string.Format("更新工作站[{0}]!", topicData.id));
        }
        private void WorkStationOnlineProcess(TopicData topicData)
        {
            ControlCenterViewModel cvm = ControlCenterManager.Instance.GetById(topicData.id);
            if (cvm != null)
            {
                cvm.IsOnline = true;
                MessageManager.Instance.Info(string.Format("指挥中心{0}上线！", cvm.StaticData.WorkStationId));
            }
            WorkStationViewModel wvm = WorkStationManager.Instance.GetById(topicData.id);
            if (wvm != null)
            {
                wvm.IsOnline = true;
                MessageManager.Instance.Info(string.Format("工作站{0}上线！", wvm.StaticData.WorkStationId));
            }
        }
        private void WorkStationOfflineProcess(TopicData topicData)
        {
            ControlCenterViewModel cvm = ControlCenterManager.Instance.GetById(topicData.id);
            if (cvm != null)
            {
                cvm.IsOnline = false;
                MessageManager.Instance.Info(string.Format("指挥中心{0}下线！", cvm.StaticData.WorkStationId));
            }
            WorkStationViewModel wvm = WorkStationManager.Instance.GetById(topicData.id);
            if (wvm != null)
            {
                wvm.IsOnline = false;
                MessageManager.Instance.Info(string.Format("工作站{0}下线！", wvm.StaticData.WorkStationId));
            }
        }
        public class WorkStationHeartBeat
        {
            public string ip { get; set; }
            public double latitude { get; set; }
            public double longitude { get; set; }
        };
        private void WorkStationHeartbeatProcess(TopicData topicData)
        {
            ControlCenterViewModel cvm = ControlCenterManager.Instance.GetById(topicData.id);
            if (cvm != null)
            {
                WorkStationHeartBeat heartbeat = JsonConvert.DeserializeObject<WorkStationHeartBeat>(topicData.data);
                cvm.HeartBeat(heartbeat.longitude, heartbeat.latitude);
            }
            WorkStationViewModel wvm = WorkStationManager.Instance.GetById(topicData.id);
            if (wvm != null)
            {
                WorkStationHeartBeat heartbeat = JsonConvert.DeserializeObject<WorkStationHeartBeat>(topicData.data);
                wvm.HeartBeat(heartbeat.longitude, heartbeat.latitude);
            }
        }
        private void WorkStationConnectedProcess(TopicData topicData)
        {
            ControlCenterViewModel cvm = ControlCenterManager.Instance.GetById(topicData.id);
            if (cvm != null)
            {
                cvm.IsOnline = true;
                MessageManager.Instance.Info(string.Format("指挥中心{0}建立连接！", cvm.StaticData.WorkStationId));
            }
            WorkStationViewModel wvm = WorkStationManager.Instance.GetById(topicData.id);
            if (wvm != null)
            {
                wvm.IsOnline = true;
                MessageManager.Instance.Info(string.Format("工作站{0}建立连接！", wvm.StaticData.WorkStationId));
            }
        }
        private void WorkStationDisconnectedProcess(TopicData topicData)
        {
            ControlCenterViewModel cvm = ControlCenterManager.Instance.GetById(topicData.id);
            if (cvm != null)
            {
                cvm.IsOnline = false;
                MessageManager.Instance.Info(string.Format("指挥中心{0}断开连接！", cvm.StaticData.WorkStationId));
            }
            WorkStationViewModel wvm = WorkStationManager.Instance.GetById(topicData.id);
            if (wvm != null)
            {
                wvm.IsOnline = false;
                MessageManager.Instance.Info(string.Format("工作站{0}断开连接！", wvm.StaticData.WorkStationId));
            }
        }
    }
}
